package com.example.ogtest.test.class12.t4;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.example.ogtest.test.tool.BufferTool;

import android.opengl.GLSurfaceView.Renderer;

public class MyGlRenderer4 implements Renderer {

	float rotateTri, rotateQuad;
	
	 int one = 0x10000;
		
		private int[] colorBufferForQuad = new int[]{
				
				 0,one,0,one,
				 0,one,0,one,
				 0,one,0,one,
				 0,one,0,one,
				 
				 one, one/2, 0, one,
				 one, one/2, 0, one,
				 one, one/2, 0, one,
				 one, one/2, 0, one,
				 
				 one,0,0,one,
				 one,0,0,one,
				 one,0,0,one,
				 one,0,0,one,
				 
				 one,one,0,one,
				 one,one,0,one,
				 one,one,0,one,
				 one,one,0,one,
				 
				 0,0,one,one,
				 0,0,one,one,
				 0,0,one,one,
				 0,0,one,one,
				 
				 one,0,one,one,
				 one,0,one,one,
				 one,0,one,one,
				 one,0,one,one,
		};
		
		 private int[] colorBuffer = new int[]{
				//tri 4 face
				 one,0,0,one,
				 0,one,0,one,
				 0,0,one,one,	
				 
				 one,0,0,one,
				 0,one,0,one,
				 0,0,one,one,
				 
				 one,0,0,one,
				 0,one,0,one,
				 0,0,one,one,
				 
				 one,0,0,one,
				 0,one,0,one,
				 0,0,one,one, 
		 };
		 
		 
		 private int[] triggerBuffer = new int[]{
				0,one,0,
				-one,-one,0,
				one,-one,one,
				
				0,one,0,
				one,-one,one,
				one,-one,-one,
				
				0,one,0,
				one,-one,-one,
				-one,-one,-one,
				
				0,one,0,
				-one,-one,-one,
				-one,-one,one
		 
		 };
		 private int[] quaterBuffer =new int[]{
					one,one,-one,
					-one,one,-one,
					one,one,one,
					-one,one,one,
					
					one,-one,one,
					-one,-one,one,
					one,-one,-one,
					-one,-one,-one,
					
					one,one,one,
					-one,one,one,
					one,-one,one,
					-one,-one,one,
					
					one,-one,-one,
					-one,-one,-one,
					one,one,-one,
					-one,one,-one,
					
					-one,one,one,
					-one,one,-one,
					-one,-one,one,
					-one,-one,-one,
					
					one, one, -one,
					one, one, one,
					one, -one, -one,
					one, -one, one,
		 };
	@Override
	public void onDrawFrame(GL10 gl)
	{
		// 清除屏幕和深度缓存
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// 重置当前的模型观察矩阵
		gl.glLoadIdentity();


		// 左移 1.5 单位，并移入屏幕 6.0
		gl.glTranslatef(-1.5f, 0.0f, -6.0f);
		 //设置旋转
		gl.glRotatef(rotateTri, 0.0f, 1.0f, 0.0f);
		
		//设置定点数组
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		//设置颜色数组
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		
		gl.glColorPointer(4, GL10.GL_FIXED, 0, BufferTool.bufferUtil(colorBuffer));
		// 设置三角形顶点
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferTool.bufferUtil(triggerBuffer));
		//绘制三角锥
	    for(int i=0; i<4; i++)
	    {
	    	gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i*3, 3);
	    }
		
		/* 渲染正方体 */
		// 重置当前的模型观察矩阵
	    gl.glLoadIdentity();
	    
	    // 左移 1.5 单位，并移入屏幕 6.0
	    gl.glTranslatef(1.5f, 0.0f, -6.0f);
	    
	    //设置旋转
	    gl.glRotatef(rotateQuad, 1.0f, 0.0f, 0.0f);
	    
	    //设置和绘制正方形
	    gl.glColorPointer(4, GL10.GL_FIXED, 0, BufferTool.bufferUtil(colorBufferForQuad));
	    gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferTool.bufferUtil(quaterBuffer));

	    for(int i=0; i<6; i++)
	    {
	    	gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i*4, 4);
	    }
	    
	    //绘制正方形结束
	    gl.glFinish();

	    //取消顶点数组
	    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	    
	    //改变旋转的角度
	    rotateTri += 0.5f;
	    rotateQuad -= 0.5f;
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height)
	{
		// TODO Auto-generated method stub
		float ratio = (float) width / height;
		//设置OpenGL场景的大小
		gl.glViewport(0, 0, width, height);
		//设置投影矩阵
		gl.glMatrixMode(GL10.GL_PROJECTION);
		//重置投影矩阵
		gl.glLoadIdentity();
		// 设置视口的大小
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		// 选择模型观察矩阵
		gl.glMatrixMode(GL10.GL_MODELVIEW);	
		// 重置模型观察矩阵
		gl.glLoadIdentity();	
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		// 启用阴影平滑
		gl.glShadeModel(GL10.GL_SMOOTH);
		
		// 黑色背景
		gl.glClearColor(0, 0, 0, 0);
		
		// 设置深度缓存
		gl.glClearDepthf(1.0f);							
		// 启用深度测试
		gl.glEnable(GL10.GL_DEPTH_TEST);						
		// 所作深度测试的类型
		gl.glDepthFunc(GL10.GL_LEQUAL);							
		
		// 告诉系统对透视进行修正
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
	}

}
